package com.lanren.familybookapp.ui.main.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.lanren.common.utils.LogUtils;
import com.lanren.common.utils.Utils;
import com.lanren.common.widget.statusbar.BarHide;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.ui.main.interfaces.IWelcomeModel;
import com.lanren.familybookapp.ui.main.presenter.WelcomePresenter;
import com.lanren.familybookapp.ui.user.view.LoginActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ClassName WelcomeActivity
 * @Description TODO 欢迎页
 * @Author chongheng.wang
 * @Date 2019/10/3 13:18
 * @Version 1.0
 */
public class WelcomeActivity extends BaseUiActivity<WelcomePresenter> implements IWelcomeModel.IWelcomeView {
    @BindView(R.id.time)
    TextView time;

    @Override
    public WelcomePresenter createPresenter() {
        return new WelcomePresenter();
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .fullScreen(true)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void doCreate(Bundle savedInstanceState) {
        initView();
    }

    @Override
    public void navigateToHome() {
        if (XXPermissions.isHasPermission(WelcomeActivity.this, Permission.Group.STORAGE) && XXPermissions.isHasPermission
                (WelcomeActivity.this, Permission.CAMERA)) {
            jumpMain();
        } else {
            requestPermission();
        }
    }

    @Override
    public void showError(int code, String msg) {
        super.showError(code, msg);
    }

    @Override
    public void initView() {
        time.setVisibility(View.GONE);
        if (AppUserHelper.getInstance().isLogin()) {
            mPresenter.checkTokenLogin();
        } else {
            onSuccess();
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showTimeTxt(long times) {
        time.setText("跳过 " + times + " ");
    }

    @Override
    public void onSuccess() {
        mPresenter.startTime();
        time.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.time)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.time:
                LogUtils.d("CountDownTimer onClick");
                if (Utils.isFastClick(view)) {
                    mPresenter.finishTime();
                }
                break;
        }
    }

    private void jumpMain() {
        if (AppUserHelper.getInstance().isLogin()) {
            startActivityFinish(MainActivity.class);
        } else {
            startActivityFinish(LoginActivity.class);
        }
    }

    private void requestPermission() {
        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                //.constantRequest()
                // 支持请求6.0悬浮窗权限8.0请求安装权限
                //.permission(Permission.REQUEST_INSTALL_PACKAGES)
                // 不指定权限则自动获取清单中的危险权限
                .permission(Permission.Group.STORAGE)
                .permission(Permission.CAMERA)
                .request(new OnPermission() {

                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll) {
//                            ToastUtils.show("获取权限成功");
                        } else {
                            ToastUtils.show("获取权限成功，部分权限未正常授予");
                        }
                        jumpMain();
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if (quick) {
                            ToastUtils.show("被永久拒绝授权，请手动授予权限");
                            //如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.gotoPermissionSettings(WelcomeActivity.this);
                        } else {
                            ToastUtils.show("获取权限失败");
                            isHasPermission();
                        }
                    }
                });
    }

    private void isHasPermission() {
        if (XXPermissions.isHasPermission(WelcomeActivity.this, Permission.Group.STORAGE) && XXPermissions.isHasPermission
                (WelcomeActivity.this, Permission.CAMERA)) {
            ToastUtils.show("已经获取到权限，不需要再次申请了");
        } else {
            requestPermission();
            ToastUtils.show("还没有获取到权限或者部分权限未授予");
        }
    }
}
